<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 订单数据
 */
namespace app\ais\model;
use app\common\model\SystemUserLevel;
use think\Model;

class AisShopOrder extends Model{
    
    protected $pk    = 'id';
    protected $type = [
        'shop_cache' => 'array',
    ];
    
    /**
     * @return \think\model\relation\HasOne
     * 商品
     */
    public function shop(){
        return $this->hasOne('AisShop','id','shop_id');
    }

    /**
     * @return \think\model\relation\HasOne
     * 店铺
     */
    public function store(){
        return $this->hasOne('AisStore','id','store_id');
    }

     /**
     * @return \think\model\relation\hasMany
     * 店铺管理员类别
     */
    public function storeWorker(){
        return $this->hasMany('AisStoreWorker','store_id','store_id');
    }

    /**
     * @return \think\model\relation\HasOne
     * 用户
     */
    public function user(){
        return $this->hasOne('app\common\model\SystemUser','id','uid');
    }

    /**
     * 订单是否核销
     */
    public function getStatuAttr($value,$data){
        $statu = [0 =>'待核销',1 =>'已核销'];
        return $statu[$data['status']];
    }


    /**
     * @param $data
     * @param $order_no
     * 购买商品生成订单数据
     */
    public static function createOrder($param,$order_no){
        $order = [
            'shop_id'           => $param['id'],
            'store_id'          => $param['store_id'],
            'member_miniapp_id' => $param['member_miniapp_id'],
            'phone'             => $param['telphone'],
            'message'           => $param['message'],
            'share_uid'         => $param['share_uid'] ?: 0,
            'uid'               => $param['uid'],
            'order_no'          => $order_no,
            'points'            => $param['points'],
            'amount'            => $param['amount'],
            'sell_price'        => $param['sell_price'],
            'thrifty'           => $param['thrifty'],
            'shop_cache'        => $param['shop_cache'],
            'status'            => 0,
            'is_del'            => 0,
            'paid_at'           => 0,
            'create_time'       => time()
        ];
        return self::create($order);
    }

    public static function income($order,$total_fee,$flag){
        $allMoney = money(($total_fee - $total_fee * 6 / 1000));
        $ex = 0;
        $order->real = $allMoney;
        $store = AisStore::where(['id' => $order->store_id])->field('id,member_miniapp_id,charges,manage_uid,mch_id,name')->find();
        if(empty($store) && $flag == 'N'){
            return;
        }
        //商品信息
        $shopItme = AisShop::where(['member_miniapp_id'=>$order->member_miniapp_id,'id' => $order->shop_id])->find();
        $queen       = []; //分账队列参数
        //上级会员
        $level = SystemUserLevel::where(['user_id' => $order->uid,'level' => 1])->find();
        //发放会员收益
        if($store->manage_uid != $level->parent_id && $shopItme->share_vip_price){
            if($level){
                $money =  money($shopItme->sell_price * $shopItme->share_vip_price);
                $allMoney -= $money;
                $ex += $money;
                if($money >= 0){
                    //会员余额增加
                    $bank = AisBank::where(['uid' => $level->parent_id])->find();
                    if(empty($bank)){
                        $bank = new AisBank;
                        $bank->uid = $level->parent_id;
                        $bank->member_miniapp_id = $order->member_miniapp_id;
                        $bank->money = 0;
                    }
                    $bank->money = $bank->money + $money;
                    $bank->save();
                    AisBill::add(['miniapp_id'=>$order->member_miniapp_id,'store_id' => 0,'money' => $money,'uid'=>$level->parent_id], '优选下单会员收益');
                }
            }

        }
        //发放分享收益
        if($order->share_uid){
            $money =  money($shopItme->sell_price * $shopItme->share_price);
            $allMoney -= $money;
            $ex += $money;
            if($money >= 0){
                //会员余额增加
                $bank = AisBank::where(['uid' => $order->share_uid])->find();
                if(empty($bank)){
                    $bank = new AisBank;
                    $bank->uid = $order->share_uid;
                    $bank->member_miniapp_id = $order->member_miniapp_id;
                    $bank->money = 0;
                }
                $bank->money = $bank->money + $money;
                $bank->save();
                AisBill::add(['miniapp_id'=>$order->member_miniapp_id,'store_id' => 0,'money' => $money,'uid'=>$order->share_uid], '分享商品收益');
            }
        }
        if($allMoney > 0){
            if($store->mch_id){
                //分给商户
                $queen[] = [
                    'member_miniapp_id' => $store->member_miniapp_id,
                    'store_id'          => $store->id,
                    'uid'               => 0,
                    'mch_id'            => $store->mch_id,
                    'amount'            => $allMoney  * 100,
                    'transaction_id'    => $order->paid_no,
                    'out_order_no'      => $order->order_no,
                    'is_finish'         => 0,
                    'types'             => 1,
                    'msg'               => '结算到商户号',
                ];
            }
            if($ex > 0){
                //平台收入
                $queen[] = [
                    'member_miniapp_id' => $order->member_miniapp_id,
                    'store_id'          => 0,
                    'uid'               => 0,
                    'mch_id'            => 0,
                    'amount'            => $ex * 100,
                    'transaction_id'    => $order->paid_no,
                    'out_order_no'      => $order->order_no,
                    'is_finish'         => 0,
                    'types'             => 2,
                    'msg'               => '结算到平台',
                ];
            }
            AisQueen::createQueen($queen);
            if($shopItme->warehouse_num >= 0){
                $shopItme->warehouse_num = $shopItme->warehouse_num - 1;
                $shopItme->save();
            }
        }
        $order->real = $allMoney;
        return $order;
    }
}
